﻿#include "stdafx.h"
#include "XHwndIntParser.h"

#include "XBSTR.h"
#include "XString.h"
#include "StrUtil.h"
#include "Util.h"
#include "IXColorDraw.h"
#include "IXImageDraw.h"
#include "IXHwnd.h"

#include "ParseHelper.h"


CXHwndIntParser::CXHwndIntParser() : CXIntParserBase(&TIntCreateControl<IXHwnd>)
{
    ;
}

CXHwndIntParser::~CXHwndIntParser()
{
    ;
}

// 创建控件
BOOL CXHwndIntParser::PreCreate(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    if(!m_ParentParser.PreCreate(pNode, pControl))
        return FALSE;

    BOOL bResult = FALSE;
    do
    {
        IXHwnd* pHwnd = Util::QueryIntf<IXHwnd>(pControl);
        if(pHwnd == NULL)
        {
            break;
        }

        XBSTR bstrTemp;

        // Style
        HRESULT hResult = pNode->GetValue(_T("style"), &bstrTemp);
        if(SUCCEEDED(hResult))
        {
            struct stStyleInfo
            {
                LPCTSTR szStyleName;
                DWORD   dwStyle;
            } data[] =
            {
                _T("minbox"),   WS_MINIMIZEBOX,
                _T("maxbox"),   WS_MAXIMIZEBOX,
                _T("sysmenu"),  WS_SYSMENU,
                _T("vscroll"),  WS_VSCROLL,
                _T("hscroll"),  WS_HSCROLL,
            };
            XString strStyle(bstrTemp);
            size_t handle = 0;
            XString strToken;
            while(StrUtil::NextToken(strStyle, handle, _T(";"), strToken, FALSE))
            {
                for(int i=0; i<_countof(data); ++ i)
                {
                    if(_tcsicmp(data[i].szStyleName, strToken.GetData()) == 0)
                    {
                        pHwnd->ModifyStyle(data[i].dwStyle, 0);
                        break;
                    }
                }
            }
        }

        // ExtendedStyle
        hResult = pNode->GetValue(_T("exstyle"), &bstrTemp);
        if(SUCCEEDED(hResult))
        {
            struct stExStyleInfo
            {
                LPCTSTR szExStyleName;
                DWORD   dwExStyle;
            } data[] =
            {
                {_T("layered"),      WS_EX_LAYERED},
                {_T("topmost"),      WS_EX_TOPMOST},
                {_T("transparent"),  WS_EX_TRANSPARENT},
            };
            XString strStyle(bstrTemp);
            size_t handle = 0;
            XString strToken;
            while(StrUtil::NextToken(strStyle, handle, _T(";"), strToken, FALSE))
            {
                for(int i=0; i<_countof(data); ++ i)
                {
                    if(_tcsicmp(data[i].szExStyleName, strToken.GetData()) == 0)
                    {
                        pHwnd->ModifyExtendStyle(data[i].dwExStyle, 0);
                        break;
                    }
                }
            }
        }

        bResult = TRUE;
    } while(FALSE);

    return bResult;
}

// 填充属性
BOOL CXHwndIntParser::FillAttr(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    BOOL bResult = m_ParentParser.FillAttr(pNode, pControl);
    if(!bResult)
        return bResult;

    XBSTR bstrTemp;

    IXHwnd* pHwnd = Util::QueryIntf<IXHwnd>(pControl);
    if(pHwnd == NULL)
        return FALSE;

    // BkBrush
    if(SUCCEEDED(pNode->GetValue(_T("bkbrush"), &bstrTemp)))
    {
        COLORREF clrBrush = RGB(255, 255, 255);
        if(ParseHelper::ParseColor(bstrTemp, clrBrush))
        {
            XBRUSH BkBrush = (XBRUSH)::CreateSolidBrush(clrBrush);
            pHwnd->SetBackBrush(BkBrush);
        }
    }

    return bResult;
}
